package com.kaifamiao.provider;

import com.kaifamiao.model.Account;
import org.apache.ibatis.jdbc.SQL;

/**
 * @author lucien-ma
 * @date 2021/8/18
 * @apiNote
 */
public class AccountProvider {

    public static String findSelective(Account account) {
        return new SQL(){
            {
                SELECT("id", "uid", "money");
                FROM("account");
                if (account.getId() != null) {
                    WHERE("id = #{id}");
                }
                if (account.getUid() != null) {
                    WHERE("uid = #{uid}");
                }
                if (account.getMoney() != null) {
                    WHERE("money = #{money}");
                }
            }
        }.toString();
    }

    public static String insertSelective(Account account) {
        return new SQL(){
            {
                INSERT_INTO("account");
                if (account != null) {
                    if (account.getId() != null) {
                        INTO_COLUMNS("id");
                        INTO_VALUES("#{id}");
                    }
                    if (account.getUid() != null) {
                        INTO_COLUMNS("uid");
                        INTO_VALUES("#{uid}");
                    }
                    if (account.getMoney() != null) {
                        INTO_COLUMNS("money");
                        INTO_VALUES("#{money}");
                    }
                }
            }
        }.toString();
    }

    public static String updateSelective(Account account) {
        return new SQL(){
            {
                UPDATE("account");
                if (account != null) {
                    if (account.getUid() != null) {
                        SET("uid = #{uid}");
                    }
                    if (account.getMoney() != null) {
                        SET("money = #{money}");
                    }
                }
                WHERE("id = #{id}");
            }
        }.toString();
    }

    public static String deleteAccounts(Integer... ids) {
        return new SQL(){
            {
                if (ids.length > 0) {
                    DELETE_FROM("account");
                    StringBuilder idsStr = new StringBuilder();
                    for (int i = 0; i < ids.length; i++) {
                        idsStr.append(ids[i]);
                        if (i != ids.length - 1) {
                            idsStr.append(",");
                        }
                    }
                    WHERE("id in (" + idsStr.toString() + ")");
                }
            }
        }.toString();
    }
}
